package com.bandapp.rss;

import java.net.URL;
import java.util.ArrayList;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class TwitterRSSWrapper extends RSSWrapper<TwitterRSSItem> {

	public TwitterRSSWrapper(URL urlFeed) {
		super(urlFeed);			
	}

	@Override
	protected void makeFeed() {
		
		
		Document document = super.getDocument();
		NodeList root = document.getElementsByTagName("rss");
		Node rootrss = root.item(0); // <rss ...>
		NodeList listOfRssChildren = rootrss.getChildNodes();
		/*
		 * we are only interested in the children with tag name "item"
		 */
		for(int i = 0; i < listOfRssChildren.getLength();i++){
			Node child = listOfRssChildren.item(i);
			/*
			 * if you look at the xml structure you would see that there is no other child than
			 * channel, but apparently some #text slipped in the xml stream
			 * so that is also considered a child, which learns us the lesson
			 * never , ever take something for granted ;)
			 */
			if(child.getNodeName().equals("channel")){
				/*
				 * channel tag hold all the <item></item>
				 */
				NodeList listOfChannelChildren = child.getChildNodes();
				//System.out.println("channelnode found !!");
				/*
				 * here we are at the level of title, link, etc we need the ones with item
				 */
				for(int j=0; j < listOfChannelChildren.getLength();j++){
					Node child2 = listOfChannelChildren.item(j);
					String title = null;
					String pubDate = null;
					String description = null;
					String link = null;
					String twittersource = "";
					if(child2.getNodeName().equals("item")){
						//System.out.println("item found !!");
						NodeList propertiesOfItem = child2.getChildNodes();
						
						/*
						 * we have now a list of nodes : description, title, link, and pubdate						
						 */
						for(int k = 0; k < propertiesOfItem.getLength();k++){
							Node property = propertiesOfItem.item(k);
							if(property.getNodeName().contains("title")){
								title = property.getFirstChild().getNodeValue();
							}else if(property.getNodeName().contains("link")){
								pubDate = property.getFirstChild().getNodeValue();
							}else if(property.getNodeName().contains("description")){
								description = property.getFirstChild().getNodeValue();
							}else if(property.getNodeName().contains("pubDate")){
								pubDate = property.getFirstChild().getNodeValue();
							}else if(property.getNodeName().contains("twitter:source")){
								twittersource = property.getFirstChild().getNodeValue();
							}
						}
						/*
						 *TODO : make proper RSSItem constructor =D, or leave the two constructors
						 *so that a user doesnt have to remember to order of which datamembers are in
						 */
						TwitterRSSItem item = new TwitterRSSItem();
						item.setDescription(description);
						item.setTitle(title);
						item.setLink(link);
						item.setPubdate(pubDate);
						super.getItems().add(item);
					}
				}
			}
		}
	}
	
	public ArrayList<TwitterRSSItem> getItems(){
		return super.getItems();
	}

}
